Can you download incidents csv via api call

Hi there

I am trying to find the best way of creating reports in Google Sheets. I have been looking at the reporting csv that can be downloaded, which is mentioned in this post: /forum/t/reporting-analyzing-common-incidents-trends-and-patterns/1651

However, downloading this appears to be a manual process. Is there an automated way of doing this via your API or any other means?

I like the spreadsheet because it gives a good baseline of metrics that I was after anyway, and avoids the need to format the json into a csv that can be read by Sheets.

HI Adam,

Thanks for reaching out. For that particular set of information, no you cannot achieve this via the API. You can indeed list incidents, not this here, but not in the same way as you see via the Analytics.

I will send this on as a feature request to our product team however and let them know what you were looking for!

Hope this helps clarify things, please let me know if not or if you have any further questions.

Kind regards,
John

Hi John

Thanks for your reply. I have found that the rate limit for the Incidents API is limiting the number of incidents I can pull details for. In that case, can you suggest the best practice way of extracting incident information out of PD presently?

Best wishes
Adam

HI Adam,

Thanks for following up. For that, case I would suggest list incidents call here, is the best way to do this, however its worth noting it will not come out the same way as you see via the Analytics export - this is not available on the API.

Kind regards,
John

Hi John

Thanks again for your response. That’s actually the call I’m using:

`curl -X GET --header 'Accept: application/vnd.pagerduty+json;version=2' --header 'Authorization: Token token=<token>' 'https://api.pagerduty.com/incidents?since=2019-04-30&until=2019-05-07&statuses%5B%5D=triggered&statuses%5B%5D=acknowledged&statuses%5B%5D=resolved&team_ids%5B%5D=<team_id>&time_zone=UTC&include%5B%5D=users&include%5B%5D=services&include%5B%5D=first_trigger_log_entries&include%5B%5D=escalation_policies&include%5B%5D=teams&include%5B%5D=assignees&include%5B%5D=acknowledgers&include%5B%5D=priorities&include%5B%5D=response_bridge'`

But when I do this, I only get back 25 incidents, rather than a complete list. Is there any way around this?

HI Adam,

Sure thing. Many endpoints are paginated, meaning only a certain number of results will be shown at any one time. However you can get this up to 100, the link here goes into more detail on this.

Kind regards,
John

Thanks very much John. Do you have an example of a paginated request or an example of how I enter the limit param? I tried adding a –limit ‘100’ to my curl and a &limit=100 to my http call but neither worked.

Hi Adam,

curl -X GET --header ‘Accept: application/vnd.pagerduty+json;version=2’ --header ‘Authorization: Token token=xxxxxxxxxxxxxxxx’ ‘https://api.pagerduty.com/incidents?limit=100

Check out the above which should get you what you want!

Kind regards,
John

Hi John

Thanks so much for your help. I have managed to get something I am happy with working for now (if only I could convert the timestamps to non-ISO 8601 or Unix times). I have copied it below in the hope it helps others:

#! /bin/bash

today=$(date +"%Y-%m-%d")
weekago=$(date -v -7d +"%Y-%m-%d")

for i in $(seq 0 25 10000); do
  contents=$(curl -X GET --header 'Accept: application/vnd.pagerduty+json;version=2' --header 'Authorization: Token token=XXXXXXXXXXXXXX' "https://api.pagerduty.com/incidents?limit=25&offset="$i"&since="$weekago"&until="$today"&team_ids%5B%5D=XXXXX")
  echo "$contents" >> ./pd_output.json
  if grep '"more":false' <<< "$contents" ; then
echo Report complete!
break
  fi
done && jq --raw-output '(.incidents[] | [.incident_number, .title, (.service | .summary), (.first_trigger_log_entry | .summary ), (.teams[] | .summary ), .created_at, ( .last_status_change_at ), (if .last_status_change_by .type == "service_reference" then "Integration API" else .last_status_change_by .summary end)] | @csv)' ./pd_output.json > ./script_output.csv && echo '"Incident Number", "Title", "Service", "Trigger", "Team", "Incident Start", "Incident End", "Ended By"' | cat - ./script_output.csv > temp && mv temp ./script_output.csv && rm ./pd_output.json
2 Likes

Hey Adam,

Glad to hear John was able to assist. Thank you so much for relaying over your solution! Greatly appreciated. Is there anything else our team can help out with?

Cheers,

Hi Charlotte, no, I think you’ve answered all my questions for now.

Thanks very much again.

Adam